<template>
  <el-dialog v-model="obj.visible"
             width="50%"
             :show-close="false"
             style="padding: 0"
             fullscreen
             :close-on-click-modal="false"
             :close-on-press-escape="false"
  >
    <template #header="{ close, titleId, titleClass }">
      <div class="header">
        <span>{{ obj.title }}</span>
        <el-link @click="close" :underline="false">
          <el-icon style="color: white">
            <Close/>
          </el-icon>
        </el-link>
      </div>
    </template>
    <div style="padding: 10px 10px">
      <OrderDetail :order="order_detail"/>
      <el-card shadow="never" style="margin-top:10px;">
        <el-form :model="obj.form" ref="ruleForm" label-width="auto" :rules="obj.rules">
          <el-table :data="obj.form.table_data" stripe show-summary :summary-method="getSummaries" border
                    style="margin-bottom: 10px">
            <el-table-column prop="vm_name" label="虚拟机名称"/>
            <el-table-column prop="cpu_arch_text" label="CPU架构"/>
            <el-table-column prop="os_text" label="操作系统"/>
            <el-table-column prop="net_location_text" label="网络位置"/>
            <el-table-column prop="security_domain_text" label="安全分区"/>
            <el-table-column prop="flavor_text" label="规格(申请)"/>
            <el-table-column prop="actual_flavor_text" label="规格(实际)" min-width="150">
              <template #default="scope">
                <el-form-item v-if="obj.form.process_method===1">
                  <el-input v-model="scope.row.actual_flavor_text" readonly>
                    <template #append>
                      <el-button @click="obj.doSelectFlavor(scope.row,scope.$index)">选择</el-button>
                    </template>
                  </el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="disk_size" label="数据盘(申请)"/>
            <el-table-column prop="actual_disk_size" label="数据盘(实际)" min-width="120">
              <template #default="scope">
                <el-form-item v-if="obj.form.process_method===1" :prop="`table_data[${scope.$index}].actual_disk_size`"
                              :rules="obj.rules.actual_disk_size">
                  <el-input-number v-model="scope.row.actual_disk_size" :step="50" :min="50"
                                   :max="2000"></el-input-number>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="cluster_name" label="资源池" min-width="150">
              <template #default="{ row, $index }">
                <el-form-item v-if="obj.form.process_method===1" :prop="`table_data[${$index}].cluster_name`"
                              :rules="obj.rules.cluster_name">
                  <el-input v-model="row.cluster_name" readonly>
                    <template #append>
                      <el-button @click="obj.doSelectCluster(row,$index)">
                        选择
                      </el-button>
                    </template>
                  </el-input>
                </el-form-item>
              </template>
            </el-table-column>
          </el-table>
          <el-form-item v-if="obj.is_edit" label="处理方式" prop="process_method">
            <el-radio-group v-model="obj.form.process_method">
              <el-radio v-for="item in obj.options.process_method" :value="item.value">{{ item.label }}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="审批意见" prop="remarks">
            <el-col :span="8">
              <el-input v-model="obj.form.remarks" type="textarea" maxlength="100" show-word-limit/>
            </el-col>
          </el-form-item>
          <el-form-item>
            <el-col :span="24">
              <el-button type="primary" @click="obj.doSubmit(ruleForm)" size="large" style="width: 200px;">
                提交
              </el-button>
              <el-button @click="obj.visible=false" size="large" style="width: 200px;">取消</el-button>
            </el-col>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </el-dialog>
</template>

<script setup>
import {ref} from "vue";
import OrderDetail from "./OrderDetail.vue";

const ruleForm = ref(null)
const props = defineProps({obj: {}, order_detail: {}})
defineExpose({ruleForm})

const getSummaries = (param) => {
  const {columns, data} = param
  const sums = []
  columns.forEach((column, index) => {
    if (index === 0) {
      sums[index] = '合计'
      return
    }
    let values = []
    if (column.property === 'cpu_arch_text') values = data.map(item => Number(item["actual_vcpu"]))
    if (column.property === 'os_text') values = data.map(item => Number(item["actual_memory"]))
    if (column.property === 'net_location_text') values = data.map(item => Number(item["vcpu"]))
    if (column.property === 'security_domain_text') values = data.map(item => Number(item["memory"]))
    if (column.property === 'disk_size') values = data.map(item => Number(item['disk_size']))
    if (column.property === 'actual_disk_size') values = data.map(item => Number(item['actual_disk_size']))
    if (!values.every(value => isNaN(value))) {
      sums[index] = values.reduce((prev, curr) => {
        const value = Number(curr)
        if (!isNaN(value)) {
          return prev + curr
        } else {
          return prev
        }
      }, 0)
    } else {
      sums[index] = ''
    }
  })
  sums[6] = `${sums[3]}核|${sums[4]}GB`
  sums[5] = `${sums[1]}核|${sums[2]}GB`
  sums[7] += "GB"
  sums[8] += "GB"
  for (let i=1;i<5;i++){
    sums[i] = ''
  }
  return sums
}
</script>

<style scoped>
.header {
  color: #FFFFFF;
  height: 48px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  background-color: rgb(51.2, 126.4, 204);

}

.cell-item {
  display: flex;
  align-items: center;
}


::v-deep(.el-card__header) {
  background-color: #F2F6FC;
  color: black;
  font-weight: bold;
  height: 72px;
}


.horizontal-timeline {
  overflow-x: auto; /* 允许横向滚动（如果内容过多） */
}

.horizontal-timeline ::v-deep(.el-timeline) {
  display: flex;
  flex-direction: row;
  padding: 0;
  margin: 0;
  white-space: nowrap; /* 防止换行 */
}

.horizontal-timeline ::v-deep(.el-timeline-item) {
  flex: 1;
  min-width: 150px; /* 设置最小宽度，防止挤压 */
  padding-right: 20px;
  position: relative;
}

.horizontal-timeline ::v-deep(.el-timeline-item__tail) {
  display: none; /* 隐藏默认的垂直线 */
}

.horizontal-timeline ::v-deep(.el-timeline-item__node) {
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
}

.horizontal-timeline ::v-deep(.el-timeline-item__timestamp) {
  text-align: center;
  margin-top: 10px;
}
</style>